var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inquiry: "",
    cartpop: false, //购物车
    detailspop: false, //详情
    sum: 0, //总数
    kinds: 0, //种类
    cartlist: [], //购物车数组
    steps: ["上传处方", "完善信息", "选择药品", "医生开药"],
    stepnum: 2,
    druplist: [],
    num: 1
  },
  //下一步
  handleNextStep() {
    var that = this
    var cartlist = JSON.stringify(that.data.cartlist)
    console.log(cartlist)
    wx.navigateTo({
      url: '/hyb_yl/backstageServices/pages/drupmespay/drupmespay&cartlist=' + cartlist,
    })
  },
  //购物车
  handleShowSelectDrugs() {
    this.setData({
      cartpop: true
    })
  },
  handleClosecart() {
    this.setData({
      cartpop: false
    })
  },
  //详情
  drupdetail() {
    this.setData({
      detailspop: true
    })
  },
  //关闭详情
  handleCloseTap() {
    this.setData({
      detailspop: false
    })
  },
  //加 减
  handleNumberClick(e) {
    let types = e.currentTarget.dataset.type,
      i = e.currentTarget.dataset.dex,
      arrs = this.data.druplist,
      id = e.currentTarget.dataset.id,
      cratlist = this.data.cartlist,
      self = this
    if (types == 'add') {
      ++arrs[i].num
    } else if (types == 'reduce') {
      --arrs[i].num
    }
    cratlist.map(function (list, index) {
      if (id == list.id) {
        list.num = arrs[i].num
        if (list.num == 0) {
          cratlist.splice(index, 1)
        }
      }
    })
    if (cratlist.length == 0) {
      self.setData({
        cartpop: false
      })
    }
    this.setData({
      druplist: arrs,
      cartlist: cratlist
    })
    this.sunmfun()
    this.typefun()
  },
  //删除购物车
  deletedrup(e) {
    let i = e.currentTarget.dataset.dex,
      cratlist = this.data.cartlist,
      arrs = this.data.druplist,
      id = e.currentTarget.dataset.id
    arrs.map(function (list) {
      if (id == list.id) {
        list.num = 0
      }
    })
    cratlist.splice(i, 1)
    if (cratlist.length == 0) {
      this.setData({
        cartpop: false
      })
    }
    this.setData({
      cartlist: cratlist,
      druplist: arrs
    })
    this.sunmfun()
    this.typefun()
  },
  //获取总数
  sunmfun() {
    let arrs = this.data.druplist,
      sum = 0
    arrs.map(function (list) {
      sum += list.num
    })
    this.setData({
      sum
    })
  },
  //获取种类
  typefun() {
    let arrs = this.data.druplist,
      tsum = 0
    arrs.map(function (list) {
      if (list.num != 0) {
        ++tsum
      }
    })
    this.setData({
      kinds: tsum
    })
  },
  //选择该药
  handleOnlyAddClick(e) {
    var that = this
    var sid = e.currentTarget.dataset.sid
    let i = e.currentTarget.dataset.dex,
      arrs = this.data.druplist,
      cartlist = this.data.cartlist
    arrs[i].num = 1
    cartlist.push(arrs[i])
    console.log(arrs)
    this.setData({
      druplist: arrs,
      cartlist
    })

    this.sunmfun()
    this.typefun()

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getall()
    var bgc = wx.getStorageSync('color')
    var j_id = options.j_id
    var key_words = options.key_words
    var conets = JSON.parse(options.conets)
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    this.setData({
      key_words: key_words,
      conets: conets,
      j_id: j_id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.sunmfun()
    this.typefun()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //查询所有药品
  getall() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/goods.allgoods',
      success: function (res) {
        console.log(res)
        that.setData({
          druplist: res.data
        })
      }
    })
  }
})